/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.PlatformUI;

/**
 * This page allows to get the following information in order to create the new goal:
 *  - Conditions code
 *  - Conditions language
 * 
 * The different conditions can be enabled or disabled and only enabled 
 * conditions will be returned
 *  
 *  @author Pablo Muñoz
 *  @TODO automatic disabling of needen't conditions by goal type
 */

public class AddGoalWizardPage2 extends WizardPage {
	private Button creationConditionEnable;
	private Combo creationConditionLanguageCombo;
	private Text creationConditionCodeText;
	
	private Button dropConditionEnable;
	private Combo dropConditionLanguageCombo;
	private Text dropConditionCodeText;
	
	private Button contextConditionEnable;	
	private Combo contextConditionLanguageCombo;
	private Text contextConditionCodeText;

	private Combo recurConditionLanguageCombo;
	private Text recurConditionCodeText;
	private Button recurConditionEnable;
	
	private Combo maintainConditionLanguageCombo;
	private Text maintainConditionCodeText;
	private Button maintainConditionEnable;
	
	private Combo targetConditionLanguageCombo;
	private Text targetConditionCodeText;
	private Button targetConditionEnable;

	/**
	 * Constructor for AddGoalWizardPage2
	 * 
	 * @param pageName
	 */
	public AddGoalWizardPage2(ISelection selection) {
		super("wizardPage");
		setTitle("Add Goal Wizard");
		setDescription("Page 2: Goal conditions");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 4;
		layout.horizontalSpacing = 9;
		layout.verticalSpacing = 9;
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addGoalWizard");
		
		Label header1 = new Label(container, SWT.NULL);
		header1.setText("Condition");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		header1.setLayoutData(gd);
		Label header2 = new Label(container, SWT.NULL);
		header2.setText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		header2.setLayoutData(gd);
		Label header3 = new Label(container, SWT.NULL);
		header3.setText("Code");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		header3.setLayoutData(gd);
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Creation:");
		creationConditionLanguageCombo = new Combo(container, SWT.READ_ONLY);
		creationConditionLanguageCombo.setItems (new String [] {"jcl"});
		creationConditionLanguageCombo.select(0);
		creationConditionLanguageCombo.setToolTipText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		creationConditionLanguageCombo.setLayoutData(gd);
		creationConditionLanguageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		creationConditionCodeText = new Text(container, SWT.BORDER | SWT.MULTI);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		creationConditionCodeText.setLayoutData(gd);
		creationConditionCodeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		creationConditionEnable = new Button(container,SWT.CHECK);
		creationConditionEnable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("Context:");
		contextConditionLanguageCombo = new Combo(container, SWT.READ_ONLY);
		contextConditionLanguageCombo.setItems (new String [] {"jcl"});
		contextConditionLanguageCombo.select(0);
		contextConditionLanguageCombo.setToolTipText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		contextConditionLanguageCombo.setLayoutData(gd);
		contextConditionLanguageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		contextConditionCodeText = new Text(container, SWT.BORDER | SWT.MULTI);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		contextConditionCodeText.setLayoutData(gd);
		contextConditionCodeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		contextConditionEnable = new Button(container,SWT.CHECK);
		contextConditionEnable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("Drop:");
		dropConditionLanguageCombo = new Combo(container, SWT.READ_ONLY);
		dropConditionLanguageCombo.setItems (new String [] {"jcl"});
		dropConditionLanguageCombo.select(0);
		dropConditionLanguageCombo.setToolTipText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		dropConditionLanguageCombo.setLayoutData(gd);
		dropConditionLanguageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		dropConditionCodeText = new Text(container, SWT.BORDER | SWT.MULTI);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		dropConditionCodeText.setLayoutData(gd);
		dropConditionCodeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		dropConditionEnable = new Button(container,SWT.CHECK);
		dropConditionEnable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("Recur:");
		recurConditionLanguageCombo = new Combo(container, SWT.READ_ONLY);
		recurConditionLanguageCombo.setItems (new String [] {"jcl"});
		recurConditionLanguageCombo.select(0);
		recurConditionLanguageCombo.setToolTipText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		recurConditionLanguageCombo.setLayoutData(gd);
		recurConditionLanguageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		recurConditionCodeText = new Text(container, SWT.BORDER | SWT.MULTI);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		recurConditionCodeText.setLayoutData(gd);
		recurConditionCodeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		recurConditionEnable = new Button(container,SWT.CHECK);
		recurConditionEnable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("Maintain:");
		maintainConditionLanguageCombo = new Combo(container, SWT.READ_ONLY);
		maintainConditionLanguageCombo.setItems (new String [] {"jcl"});
		maintainConditionLanguageCombo.select(0);
		maintainConditionLanguageCombo.setToolTipText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		maintainConditionLanguageCombo.setLayoutData(gd);
		maintainConditionLanguageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		maintainConditionCodeText = new Text(container, SWT.BORDER | SWT.MULTI);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		maintainConditionCodeText.setLayoutData(gd);
		maintainConditionCodeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		maintainConditionEnable = new Button(container,SWT.CHECK);
		maintainConditionEnable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("Maintain:");
		targetConditionLanguageCombo = new Combo(container, SWT.READ_ONLY);
		targetConditionLanguageCombo.setItems (new String [] {"jcl"});
		targetConditionLanguageCombo.select(0);
		targetConditionLanguageCombo.setToolTipText("Language");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		targetConditionLanguageCombo.setLayoutData(gd);
		targetConditionLanguageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		targetConditionCodeText = new Text(container, SWT.BORDER | SWT.MULTI);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		targetConditionCodeText.setLayoutData(gd);
		targetConditionCodeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		targetConditionEnable = new Button(container,SWT.CHECK);
		targetConditionEnable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		initialize();
		dialogChanged();
		setControl(container);
	}

	/*
	 * This method is called in order to initialize the controls status
	 */
	private void initialize() {
		enabledPropertiesChanged();
	}

	/*
	 * Ensures that all enabled text fields are set.
	 */
	private void dialogChanged() {
		if(creationConditionCodeText.isEnabled() && creationConditionCodeText.getText().length() == 0){
			updateStatus("Creation condition must be specified or disabled");
			return;
		}
		if(contextConditionCodeText.isEnabled() && contextConditionCodeText.getText().length() == 0){
			updateStatus("Context condition must be specified or disabled");
			return;
		}
		if(dropConditionCodeText.isEnabled() && dropConditionCodeText.getText().length() == 0){
			updateStatus("Drop condition must be specified or disabled");
			return;
		}
		if(recurConditionCodeText.isEnabled() && recurConditionCodeText.getText().length() == 0){
			updateStatus("Recur condition must be specified or disabled");
			return;
		}
		if(maintainConditionCodeText.isEnabled() && maintainConditionCodeText.getText().length() == 0){
			updateStatus("Maintain condition must be specified or disabled");
			return;
		}
		if(targetConditionCodeText.isEnabled() && targetConditionCodeText.getText().length() == 0){
			updateStatus("Target condition must be specified or disabled");
			return;
		}
		updateStatus(null);
	}
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
		creationConditionLanguageCombo.setEnabled(creationConditionEnable.getSelection());
		creationConditionCodeText.setEnabled(creationConditionEnable.getSelection());
		contextConditionLanguageCombo.setEnabled(contextConditionEnable.getSelection());
		contextConditionCodeText.setEnabled(contextConditionEnable.getSelection());
		dropConditionLanguageCombo.setEnabled(dropConditionEnable.getSelection());
		dropConditionCodeText.setEnabled(dropConditionEnable.getSelection());
		recurConditionLanguageCombo.setEnabled(recurConditionEnable.getSelection());
		recurConditionCodeText.setEnabled(recurConditionEnable.getSelection());
		maintainConditionLanguageCombo.setEnabled(maintainConditionEnable.getSelection());
		maintainConditionCodeText.setEnabled(maintainConditionEnable.getSelection());
		targetConditionLanguageCombo.setEnabled(targetConditionEnable.getSelection());
		targetConditionCodeText.setEnabled(targetConditionEnable.getSelection());	
		dialogChanged();
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/**
	 * @return creation condition language or null if disabled
	 */
	public String getCreationConditionLanguage() {
		if(creationConditionEnable.getSelection()){
			return creationConditionLanguageCombo.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return creation condition code or null if disabled
	 */
	public String getCreationConditionCode() {
		if(creationConditionEnable.getSelection()){
			return creationConditionCodeText.getText();
		}
		return null;
	}

	/**
	 * @return drop condition language or null if disabled 
	 */
	public String getDropConditionLanguage() {
		if(dropConditionEnable.getSelection()){
			return dropConditionLanguageCombo.getText();
		}
		return null;
	}
	
	/**
	 * 
	 * @return drop condition code or null if disabled
	 */
	public String getDropConditionCode() {
		if(dropConditionEnable.getSelection()){
			return dropConditionCodeText.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return context condition language or null if disabled
	 */
	public String getContextConditionLanguage() {
		if(contextConditionEnable.getSelection()){
			return contextConditionLanguageCombo.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return context condition code or null if disabled
	 */
	public String getContextConditionCode() {
		if(contextConditionEnable.getSelection()){
			return contextConditionCodeText.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return recur condition language or null if disabled
	 */
	public String getRecurConditionLanguage() {
		if(recurConditionEnable.getSelection()){
			return recurConditionLanguageCombo.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return recur condition code or null if disabled
	 */
	public String getRecurConditionCode() {
		if(recurConditionEnable.getSelection()){
			return recurConditionCodeText.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return maintain condition language or null if disabled
	 */
	public String getMaintainConditionLanguage() {
		if(maintainConditionEnable.getSelection()){
			return maintainConditionLanguageCombo.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return maintain condition code or null if disabled
	 */
	public String getMaintainConditionCode() {
		if(maintainConditionEnable.getSelection()){
			return maintainConditionCodeText.getText();
		}
		return null;
	}


	/**
	 * 
	 * @return target condition language or null if disabled
	 */
	public String getTargetConditionLanguage() {
		if(targetConditionEnable.getSelection()){
			return targetConditionLanguageCombo.getText();
		}
		return null;
	}

	/**
	 * 
	 * @return target condition code or null if disabled
	 */
	public String getTargetConditionCode() {
		if(targetConditionEnable.getSelection()){
			return targetConditionCodeText.getText();
		}
		return null;
	}
}